﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class Default2 : System.Web.UI.Page
{
    public int p;
    public int pais;
    public int id = 0;
    public DataTable bank = null;
    public ArrayList Bankarr = null;
    //public ArrayList Coun = null;
    public int countryId;
    public ArrayList banks = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lb_error.Visible = false;
            lb_camp.Visible = false;

                Bankarr = (ArrayList)DB.getArrBancosCountry(id);
                if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
                lb_Bankid.Text = id.ToString();

                    ArrayList paises = DB.getArrCountries("");
                    llenocountries();
                            
                            RE_GenericBean banks = DB.getBank(id);
                            
                            lb_Bankid.Text = banks.intC1.ToString();
                            tb_bank.Text = banks.strC1;
                            DropDownList1.SelectedValue = banks.intC2.ToString();
                            tb_description.Text = banks.strC3;
                            
                            
            if (banks.intC1 != 0)
                {
                    {
                        LinkButton4_ModalPopupExtender.Show();
                    }
                }
            else
                {
                    id = int.Parse(lb_Bankid.Text);
                }
        }
        else
                {

                    lb_error.Visible = false;
                    lb_camp.Visible = false;
                    Bankarr = (ArrayList)DB.getArrBancosCountry(id);
                    
                }
    }

    private void llenocountries()
    {
        ArrayList country = null;
        ListItem item = null;
        
        DropDownList1.Items.Add("-Select one-");
        try
        {
            country  = (ArrayList)DB.getArrCountries("");
            if (country != null && country.Count > 0)
            {   
                foreach (RE_GenericBean coun in country)
                {
                    item = new ListItem(coun.strC1, coun.intC1.ToString());
                    DropDownList1.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }
    
    private void habilitocampos(bool estado)
    {
        tb_bank.Enabled = estado;
        tb_description.Enabled = estado;
        //tb_numberemployee.Enabled = estado;
        DropDownList1.Enabled = estado;
        
    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {                                                                      //(Int32.Parse(lb_Bankid.Text))        
        p = DB.getCountBankEmployee(int.Parse(lb_Bankid.Text));

        
            if (p == 0)
            {
                int id = int.Parse(lb_Bankid.Text);
                if (id != 0)
                {
                    int result = DB.DeleteBank(id);
                    Response.Redirect("Bank.aspx");
                }
            }
            else
            {
                lb_error.Visible = true;
                LinkButton4_ModalPopupExtender.Show();
                
            }
        
        
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (tb_bank.Text == "" || tb_description.Text == ""  || DropDownList1.SelectedIndex == 0)//|| DropDownList2.SelectedIndex == 0 || DropDownList1.SelectedIndex == 0
            {
                LinkButton4_ModalPopupExtender.Show();
                lb_camp.Visible = true;
                
            }
        else
        {

            RE_GenericBean banks = new RE_GenericBean();

            bool edit = false;
            banks.intC1 = int.Parse(lb_Bankid.Text);
            banks.strC1 = tb_bank.Text.Trim().ToUpper();
            banks.intC2 = DropDownList1.SelectedIndex;
            banks.strC3 = tb_description.Text.Trim().ToUpper();
            

            if (banks.intC1 != 0) edit = true;
            if (DB.InsertBank(banks, edit))
            {
                habilitocampos(false);
                Response.Redirect("Bank.aspx");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        LinkButton4_ModalPopupExtender.Show();
        lb_error.Visible = false;
        tb_description.Text = "";
        tb_bank.Text = "";
        tb_bank.Text.Remove(0);
        tb_description.Text.Remove(0);
        habilitocampos(false);
    }

    protected void LinkButton4_Click(object sender, EventArgs e)
    { 
        tb_bank.Text.Remove(0);
        tb_description.Text.Remove(0);
        LinkButton3.Enabled = true;
        lb_error.Visible = false;
        tb_description.Text = " ";
        tb_bank.Text = " ";
        habilitocampos(false);
    }

    protected void btnCuentaCancelar_Click(object sender, EventArgs e)
    {
        habilitocampos(false);
        tb_description.Text = " ";
        tb_bank.Text = " ";
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        tb_description.Text = " ";
        tb_bank.Text = " ";
        Response.Redirect("Bank.aspx");
    }
   
}